 

#import "addStockInWatchListSetUp.h"
#import "AppDelegate.h"
@implementation addStockInWatchListSetUp


@synthesize delegate_UIPopoverController;
@synthesize delegate,AppleDeglate  ; 

#pragma mark -
#pragma mark  物件初始化
 
-(id)initWithallcountry:(NSMutableArray *)allcountrys {
	self= [super init];
	if (self) {
		allcountry=allcountrys;
	}
	return self;	 	
}
 
#pragma mark -
#pragma mark  啓動畫面開始程式
- (void)loadView {
	 
    UINavigationBar *nav;
    
    UIView *mainview;        
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
        
        
        if ( [[(AppDelegate * )AppleDeglate interfaceOrientation_ipad] isEqualToString:@"UIInterfaceOrientationPortrait 垂直"] || [[(AppDelegate * )AppleDeglate interfaceOrientation_ipad] isEqualToString: @"UIInterfaceOrientationPortraitUpsideDown 垂直"] ){
            //垂直
            mainview=[[UIView alloc]initWithFrame:CGRectMake(0.0f, 0.0f, 600.0f, 600.0f)];
            self.view=mainview;
            [mainview release];
            self.view.backgroundColor=[UIColor lightTextColor];	
            
            nav = [[UINavigationBar alloc] initWithFrame: CGRectMake(0.0f, 0.0f, 540.0f, 48.0f)];	
            myTable = [[UITableView alloc] initWithFrame:CGRectMake(0.0f, 48.0f, 540.0f, 580.0f)  style:UITableViewStyleGrouped];                
            
        }
        else{
            //水平
            mainview=[[UIView alloc]initWithFrame:[UIScreen mainScreen].applicationFrame];
            self.view=mainview;
            [mainview release];
            
            self.view.backgroundColor=[UIColor lightTextColor];	
            
            nav = [[UINavigationBar alloc] initWithFrame: CGRectMake(0.0f, 0.0f, 320.0f, 48.0f)];	
            myTable = [[UITableView alloc] initWithFrame:CGRectMake(0.0f, 48.0f, 320.0f, 412.0f)  style:UITableViewStyleGrouped];                
        }
        
        
        
        
        
    }
    else{
        
        mainview=[[UIView alloc]initWithFrame:[UIScreen mainScreen].applicationFrame];
        self.view=mainview;
        [mainview release];
        
        self.view.backgroundColor=[UIColor lightTextColor];	
        
        nav = [[UINavigationBar alloc] initWithFrame: CGRectMake(0.0f, 0.0f, 320.0f, 48.0f)];	
        
        myTable = [[UITableView alloc] initWithFrame:CGRectMake(0.0f, 48.0f, 320.0f, self.view.frame.size.height)  style:UITableViewStyleGrouped];
        
        
    }
    
    
    
    
    
    
    
    
    
    
	UIBarButtonItem *barButton ;
	barButton = [[UIBarButtonItem alloc] initWithTitle:  NSLocalizedString(@"Done",@"Done")  style:UIBarButtonItemStylePlain target:self action:@selector(slideOut)];
	UINavigationItem *item;
	 item = [[UINavigationItem alloc] initWithTitle:   NSLocalizedString(@"Search Setup",@"Search Setup") ];
	[item setRightBarButtonItem: barButton];
	[nav pushNavigationItem:item animated:NO];	
	[item release];
         
         
    
    
    
	myTable.dataSource = self;	//連結資料 UITableViewDataSource
	myTable.delegate=self; //連結動作 UITableViewDelegate	
	[self.view addSubview:myTable];
    [mainview addSubview:nav ];	
	[nav release];
    
	//三個選項
	SearchCondition = [[NSArray arrayWithObjects:NSLocalizedString(@"Search By Name",@"Search By Name"), nil] retain];	
    SearchCountry =allcountry;
	localize=[[NSArray arrayWithObjects:NSLocalizedString(@"Name Localize",@"Name Localize"), nil] retain];	
	
}

#pragma mark -
#pragma mark  滑出
-(void)slideOut{
		
          
	
    
    
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
        //[delegate_UIPopoverController dismissPopoverAnimated:YES]; 
        
        if ( [[(AppDelegate * )AppleDeglate interfaceOrientation_ipad] isEqualToString:@"UIInterfaceOrientationPortrait 垂直"] || [[(AppDelegate * )AppleDeglate interfaceOrientation_ipad] isEqualToString: @"UIInterfaceOrientationPortraitUpsideDown 垂直"] ){
            //垂直
            [self dismissModalViewControllerAnimated:YES];  
            
            [[(AppDelegate * )AppleDeglate Master_popController] presentPopoverFromBarButtonItem: 
             [(SplitViewDelegate *)[(AppDelegate * )AppleDeglate splitViewDelegate] showmasterUIBarButtonItem]  permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];              
            
            
        }
        else{
            //水平
            [delegate_UIPopoverController dismissPopoverAnimated:YES]; 
            
            
        }
        
        
        
        
        
        
        
    }    
	else{
          
    }    
    [self.delegate aflipsideViewControllerDidFinish:self];    
    
    
    
    
}

 
#pragma mark -
#pragma mark  表格事件

//區塊數量
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
	return 3;
}
//區塊與列數量
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
	switch (section) {
		case 0:
			return [SearchCondition count];
			break;
		case 1:
			return [SearchCountry count];
			break;
		case 2:
			return [localize count];
			break;			 
	} 
	return 0;
}
//表格內容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"shows"];
	if (cell == nil) {
		cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
									   reuseIdentifier:@"shows"] autorelease];
	}
	//讓表格的列不會被使用者點選時出現藍色的狀態
	cell.selectionStyle=UITableViewCellSelectionStyleNone;
	
	switch (indexPath.section) {
		case 0:{
			    //加入股票名稱搜尋 @"Search By Name" tag=100 
				cell.textLabel.text = [SearchCondition objectAtIndex:indexPath.row];
				UISwitch *onoff;
				onoff = [[UISwitch alloc] initWithFrame: CGRectZero];
				onoff.tag=100;
				[onoff addTarget: self action: @selector(switchChange:) forControlEvents: UIControlEventValueChanged];
				
				//取出設定檔資料
				NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
				[prefs  boolForKey:@"Search By Name" ];
				
				onoff.on=(BOOL)[prefs  boolForKey:@"Search By Name" ];
				
				[self.view addSubview: onoff];
				cell.accessoryView=onoff;
				[onoff release];
				break;
		 }
		case 1:{
			cell.textLabel.text =NSLocalizedString([SearchCountry objectAtIndex:indexPath.row],[TechIndicator_1 objectAtIndex:indexPath.row]) ;
			
			UISwitch *onoff;
			onoff = [[UISwitch alloc] initWithFrame: CGRectZero];
			onoff.tag=200 + indexPath.row ;
			[onoff addTarget: self action: @selector(switchChange:) forControlEvents: UIControlEventValueChanged];
			
			NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
			NSMutableArray *a;
			//a=[[NSMutableArray alloc] init];
			a=[prefs objectForKey:@"Search Country"];			
			
			if ([a indexOfObject:[allcountry objectAtIndex:indexPath.row]]==2147483647) {
				//表示無
				onoff.on=NO;
			}
			else{
				onoff.on=YES;
			}
			
			[self.view addSubview: onoff];
			cell.accessoryView=onoff;
			[onoff release];
			
			break;
		}
		case 2:{
			//加入股票名稱搜尋 @"Search By Name" tag=100 
			cell.textLabel.text = [localize objectAtIndex:indexPath.row];
			UISwitch *onoff;
			onoff = [[UISwitch alloc] initWithFrame: CGRectZero];
			onoff.tag=50;
			[onoff addTarget: self action: @selector(switchChange:) forControlEvents: UIControlEventValueChanged];
			
			//取出設定檔資料
			NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
			[prefs  boolForKey:@"localize Name" ];
			 
			onoff.on=(BOOL)[prefs  boolForKey:@"localize Name" ];
			
			[self.view addSubview: onoff];
			cell.accessoryView=onoff;
			[onoff release];
			break;
		}
			
			
		 
	}
	//cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
	
	return cell;
}
//表格頭標題
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
	
	NSString *title = nil;
	switch (section) {
		case 0:
			title =NSLocalizedString(@"Search Condition",@"Search Condition");
			break;
		case 1:
			title =NSLocalizedString(@"Search Country",@"Search Country");
			break;
		case 2:
			title =NSLocalizedString(@"Localize Language",@"Localize Language");
			break;			
			
		default:
			break;
	}
	return title;
}
//表格底標題
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
	NSString *title = nil;
	switch (section) {
		case 0:
			title = NSLocalizedString(@"Search by name will slow down search time.",@"Search by name will slow down search time.");
			break;
		case 1:
			title = NSLocalizedString(@"At least one must be selected.",@"At least one must be selected.");
			break;
		case 2:
			title = NSLocalizedString(@"If turn on ,the Name will use localize language.",@"If turn on ,the Name will use localize language.");
			break;
			
			
		default:
			break;
	}
	return title;	
	
	
}

#pragma mark -
#pragma mark  按鈕轉換
-(void)switchChange:(id) sender{
	
	UISwitch *onoff = (UISwitch *) sender;	
	if (onoff.tag>=200) {
		//搜尋國家的選擇
		
		NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
		NSMutableArray *a;
		//a=[[NSMutableArray alloc] init];
		a= [prefs objectForKey:@"Search Country"]  ;
		
		if (onoff.on==YES) {
			[a addObject:[allcountry objectAtIndex: onoff.tag-200]];
			
		}
		else {
			[a removeObject:[allcountry objectAtIndex: onoff.tag-200]];			
			//至少國家必須有一個
			if ([a count]==0) {
				onoff.on=YES;
				[a addObject:[allcountry objectAtIndex: onoff.tag-200]];
				UIAlertView *al;
				al=[[UIAlertView alloc] initWithTitle:@"Warring !" message:@"Must have least one Country" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil];
				[al show];
			}
 			
		}
		
		[prefs setObject:a forKey:@"Search Country"];
		[prefs synchronize];		
	}
	else {
		
		
		switch (onoff.tag) {
			case 100:{
				//搜尋是否要使用名稱
				NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
				[prefs setBool:onoff.on  forKey:@"Search By Name"];
				[prefs synchronize];	//將記憶體中的資料存到硬體上 儲存指令 			 
				break;
			}
			case 50:{
				//名稱是否在地化
				NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
				[prefs setBool:onoff.on  forKey:@"localize Name"];
				[prefs synchronize];	//將記憶體中的資料存到硬體上 儲存指令 			 
				break;
			}				
				
			default:
				break;
		}
	 	
	}
	//[NSUserDefaults synchronize];		
	
	
	
}
#pragma mark -
#pragma mark  其他

- (void)dealloc {	
	
	[myTable release];
	[SearchCondition release];
	[SearchCountry release];
	[super dealloc];
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}




@end
